﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using UsernameAssertionLibrary;

using Microsoft.Web.Services3;
using Microsoft.Web.Services3.Design;


namespace Incubator
{
    public partial class login : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] == null || (int)Session["UserID"] == -1)
                Session["UserID"] = -1;
            if ((int)Session["UserID"] <= 0)
            {
                this.logouttable.Visible = false;
                this.logintable.Visible = true;
            }
            else
            {//login success
                this.logouttable.Visible = true;
                this.logintable.Visible = false;
                this.lblUsername.Text = ((webRef.User)Session["User"]).username;
            }
           
        }
        //Login
        protected void Button1_Click1(object sender, EventArgs e)
        {

            UsernameClientAssertion assert = new UsernameClientAssertion(usernameTB.Text, passwordTB.Text);

            // create policy
            Policy policy = new Policy();
            policy.Assertions.Add(assert);
            Global.incuServ.SetPolicy(policy);

            try
            {
                Session["UserID"] = Global.incuServ.VerifyUser(usernameTB.Text, passwordTB.Text);
                //returns 0 if user does not exist, -1 if password is incorrect, returns user id otherwise
                if ((int)Session["UserID"] == -1)
                {//password incorrect
                    this.lblError.Text = "Incorrect Password";
                }
                else if ((int)Session["UserID"] == 0)
                {
                    this.lblError.Text = "User does not exist.";
                }
                else
                {

                    Session["username"] = usernameTB.Text;
                    Session["pass"] = passwordTB.Text;
                    webRef.User u = Global.incuServ.GetUser((int)Session["UserID"]);
                    Session["User"] = u;
                    //regular user
                    if (u.role_id == 0)
                    {
                        if (u.company_id == 0)
                        {//company does not exist
                            Response.Redirect("CreateCompany.aspx");
                        }
                        else
                        {
                            Response.Redirect("Company.aspx");
                        }
                    }
                    else //admin
                    {
                        Response.Redirect("Admin.aspx");
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblError.Text = "Username or Password is incorrect.";
            }
        }
       
        //Logout
        protected void Button2_Click(object sender, EventArgs e)
        {
            Session["UserID"] = -1;
            Session["User"] = null;
            
            this.logouttable.Visible = false;
            this.logintable.Visible = true;

            Response.Redirect("main.aspx");
 
        }

       
    }
}